<?php

class UserAction extends Action {

    public function index() {
        $this->display();
    }

    //登录验证
    public function checkUser() {
        $username = trim($_REQUEST['username']);
        $passwd = trim($_REQUEST['passwd']);
        $user_model = M('mer_user');
        $user_info = $user_model->where("username='$username' and password='" . md5($passwd) . "'")->find();
        if (!empty($user_info)) {
            //记录登录日志
            $this->saveLog($user_info);
            //设置cookie
            cookie('UserName', $user_info['username'], 86400);
            cookie('UserID', $user_info['id'], 86400);
            //header("location:/Product/productList/classname/mp");
			//var_dump('abc');exit;
			header("location:".U ( 'Product/productList'));
        } else {
        	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script>alert('用户名或密码错误');history.go(-1)</script>";
        }
    }

    //退出登录
    public function exitUser() {
        cookie('UserName', NULL);
        cookie('UserID', NULL);
		header("Location:".U ( 'User/index'));
    }

    //记录登录日志
    private function saveLog($user_info) {
        $log_model = M('mer_log');
        $log_data['type'] = 'user_login';
        $log_data['content'] = 'IP:' . getIP();
        $log_data['mer_userid'] = $user_info['id'];
        $log_data['addtime'] = date('Y-m-d H:i:s');
        $log_model->data($log_data)->add();
    }

}

?>